package com.mma.assigmentcalculator;

import java.text.DecimalFormat;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Calculator extends Activity implements OnClickListener {

	private DecimalFormat df = new DecimalFormat("#.##");

	private double sum;
	private String resultString;
	private TextView calculatorTV;
	private GridLayout calculatorPad;

	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private Button button5;
	private Button button6;
	private Button button7;
	private Button button8;
	private Button button9;
	private Button button0;

	private Button buttonC;
	private Button buttonPlus;
	private Button buttonMinus;
	private Button buttonMultiply;
	private Button buttonDevide;
	private Button buttonResult;
	private Button buttonPower;
	private Button buttonSquare;
	private Button buttonPM;
	private Button buttonPow2;

	private Button buttonM, buttonMPlus, buttonMMinus, buttonMClear;

	private String operation;

	private double oldValue;

	private double savedValue;

	private TextView mPlus;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calculator);
		resultString = "0";
		calculatorTV = (TextView) findViewById(R.id.calculatorTV);

		Typeface calculatorFont = Typeface.createFromAsset(getAssets(),
				"digital-7.ttf");
		calculatorTV.setTypeface(calculatorFont);

		calculatorPad = (GridLayout) findViewById(R.id.calculatorPad);

		mPlus = (TextView) findViewById(R.id.mPlus);

		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		button4 = (Button) findViewById(R.id.button4);
		button5 = (Button) findViewById(R.id.button5);
		button6 = (Button) findViewById(R.id.button6);
		button7 = (Button) findViewById(R.id.button7);
		button8 = (Button) findViewById(R.id.button8);
		button9 = (Button) findViewById(R.id.button9);
		button0 = (Button) findViewById(R.id.button0);

		buttonC = (Button) findViewById(R.id.buttonC);
		buttonPlus = (Button) findViewById(R.id.buttonPlus);
		buttonMinus = (Button) findViewById(R.id.buttonMinus);
		buttonMultiply = (Button) findViewById(R.id.buttonMultiply);
		buttonDevide = (Button) findViewById(R.id.buttonDevide);
		buttonResult = (Button) findViewById(R.id.result);
		buttonPower = (Button) findViewById(R.id.buttonPower);
		buttonSquare = (Button) findViewById(R.id.buttonSquare);
		buttonPM = (Button) findViewById(R.id.buttonPM);
		buttonPow2 = (Button) findViewById(R.id.buttonPow2);

		buttonM = (Button) findViewById(R.id.buttonM);
		buttonMPlus = (Button) findViewById(R.id.buttonMPlus);
		buttonMClear = (Button) findViewById(R.id.buttonMClear);
		buttonMMinus = (Button) findViewById(R.id.buttonMMinus);

		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);
		button5.setOnClickListener(this);
		button6.setOnClickListener(this);
		button7.setOnClickListener(this);
		button8.setOnClickListener(this);
		button9.setOnClickListener(this);
		button0.setOnClickListener(this);

		buttonC.setOnClickListener(this);
		buttonPlus.setOnClickListener(this);
		buttonMinus.setOnClickListener(this);
		buttonMultiply.setOnClickListener(this);
		buttonDevide.setOnClickListener(this);
		buttonResult.setOnClickListener(this);
		buttonPower.setOnClickListener(this);
		buttonSquare.setOnClickListener(this);
		buttonPM.setOnClickListener(this);
		buttonPow2.setOnClickListener(this);

		buttonM.setOnClickListener(this);
		buttonMPlus.setOnClickListener(this);
		buttonMMinus.setOnClickListener(this);
		buttonMClear.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.button0:
			sum *= 10;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.button1:
			sum = sum * 10 + 1;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.button2:
			sum = sum * 10 + 2;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.button3:
			sum = sum * 10 + 3;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.button4:
			sum = sum * 10 + 4;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.button5:
			sum = sum * 10 + 5;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.button6:
			sum = sum * 10 + 6;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.button7:
			sum = sum * 10 + 7;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.button8:
			sum = sum * 10 + 8;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.button9:
			sum = sum * 10 + 9;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.result:
			calculate();
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.buttonPM:
			sum = -sum;
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.buttonPow2:
			sum = Math.pow(sum, 2);
			calculatorTV.setText(df.format(sum));
			break;
		case R.id.buttonC:
			clear();
			calculatorTV.setText(df.format(sum));
			break;

		case R.id.buttonSquare:
			operation = "sq";
			calculate();
			calculatorTV.setText(df.format(sum));
			break;

		case R.id.buttonPlus:
			operation = "+";
			calculatorTV.setText(operation);
			oldValue = sum;
			sum = 0;
			break;
		case R.id.buttonMinus:
			operation = "-";
			calculatorTV.setText(operation);
			oldValue = sum;
			sum = 0;
			break;
		case R.id.buttonMultiply:
			operation = "*";
			calculatorTV.setText(operation);
			oldValue = sum;
			sum = 0;
			break;
		case R.id.buttonDevide:
			operation = "/";
			calculatorTV.setText(operation);
			oldValue = sum;
			sum = 0;
			break;
		case R.id.buttonPower:
			operation = "^";
			calculatorTV.setText(operation);
			oldValue = sum;
			sum = 0;
			break;

		case R.id.buttonM:
			sum = savedValue;
			calculatorTV.setText(df.format(sum));
			updateM();
			break;
		case R.id.buttonMPlus:
			savedValue += sum;
			updateM();
			break;
		case R.id.buttonMMinus:
			savedValue -= sum;
			updateM();
			break;
		case R.id.buttonMClear:
			savedValue = 0;
			updateM();
			break;

		}
	}

	private void updateM() {
		if (savedValue == 0) {
			mPlus.setText("");
		} else {
			mPlus.setText("M+");
		}

	}

	private void clear() {
		sum = 0;
		oldValue = 0;

	}

	private void calculate() {
		if (operation.equals("^")) {
			sum = Math.pow(oldValue, sum);
		}
		if (operation.equals("sq")) {
			sum = Math.sqrt(sum);
		}
		if (operation.equals("+")) {
			sum += oldValue;
		}
		if (operation.equals("-")) {
			sum -= oldValue;
		}
		if (operation.equals("*")) {
			sum *= oldValue;
		}
		if (operation.equals("/")) {
			if (sum != 0) {
				sum /= oldValue;
			} else {
				// sum = 0;
				Toast.makeText(getApplicationContext(), "Devide by zero ",
						Toast.LENGTH_SHORT).show();
			}
		}
		operation = "";
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
	}

}
